<template>

    <div class="app-container">
        <div class="searchAll" slot="search">
            <el-form :model="queryParams" ref="queryForm" label-position="right" v-show="showSearch" label-width="88px">
                <el-row :gutter="20">
                    <el-col :span="6">
                        <el-form-item label="送检批次号" prop="inspectionLotMark">
                            <el-input v-model="queryParams.inspectionLotMark" :placeholder="$t('public.placeholder',{placeholder:'送检批次号'})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item :label="$t('inventoryManagement.inventoryManagement')" prop="materialCode">
                            <el-input v-model="queryParams.materialCode" :placeholder="$t('public.placeholder',{placeholder:$t('inventoryManagement.inventoryManagement')})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="数据记录时间">
                            <el-date-picker v-model="dateRange" size="small" style="width: 100%" value-format="yyyy-MM-dd" type="daterange" autocomplete="off" range-separator="-" :start-placeholder="$t('public.startDate')" :end-placeholder="$t('public.endDate')"></el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <div class="searchAres searchAresHeng" style="text-align:center">
                            <el-button type="primary" icon="el-icon-search" size="mini" class="searchBtn" @click="handleQuery">{{ $t("public.query") }}</el-button>
                            <el-button class="resectBtn" icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("public.reset") }}</el-button>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div class="tableList">
            <div class="insTitle">
                <span>IOT数据</span>
            </div>
            <!-- <div class="btnGroup">
                <el-button v-hasPermi="['tenant:operlog:remove']" type="primary" icon="el-icon-delete" size="mini" class="additemBtn" @click="handleClean">{{ $t("public.clear") }}</el-button>
                <el-button v-hasPermi="['tenant:operlog:remove']" icon="el-icon-delete" size="mini" class="deleteitemBtn" :disabled="multiple" @click="handleDelete">{{ $t("public.delete") }}</el-button>
                <el-button v-hasPermi="['tenant:operlog:export']" class="exportitemBtn" size="mini" :loading="exportLoading" @click="handleExport"><img src="../../../assets/images/icons/daochu.png" alt=""> {{ $t("public.export") }}</el-button>

            </div> -->
            <el-table height="calc(100vh - 308px)" border ref="tables" stripe v-loading="loading" :data="list" @selection-change="handleSelectionChange" :default-sort="defaultSort" @sort-change="handleSortChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="送检批次号" align="center" prop="inspectionLotMark" width="150" :show-overflow-tooltip="true" />
                <el-table-column :label="$t('inventoryManagement.inventoryManagement')" align="center" prop="materialCode" width="130" :show-overflow-tooltip="true" />
                <el-table-column label="实际测量" align="center" prop="actualMeasurement" :show-overflow-tooltip="true" />
                <el-table-column label="数据记录时间" align="center" prop="dataTime" width="150" :show-overflow-tooltip="true" />
            </el-table>

            <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" layout="total, sizes, prev, pager, next, jumper" />
        </div>

        <!-- 操作日志详细 -->
        <el-dialog :title="$t('operaLog.opLogDetail')" :visible.sync="open" width="700px" append-to-body :fullscreen="fullscreen">
            <DialogTit slot="title" :title="$t('operaLog.opLogDetail')" :fullscreen.sync="fullscreen"></DialogTit>
            <el-form ref="form" :model="form" label-width="100px" size="mini">
                <el-row>
                    <el-col :span="12">
                        <el-form-item :label="$t('operaLog.opModule')">{{ form.title }} / {{ typeFormat(form) }}</el-form-item>
                        <el-form-item :label="$t('operaLog.loginInfo')">{{ form.operName }} / {{ form.operIp }}</el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('operaLog.requestUrl')">{{ form.operUrl }}</el-form-item>
                        <el-form-item :label="$t('operaLog.method')">{{ form.requestMethod }}</el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item :label="$t('operaLog.opMethod')">{{ form.method }}</el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item :label="$t('operaLog.reqParam')">{{ form.operParam }}</el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item :label="$t('operaLog.resParam')">{{ form.jsonResult }}</el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('operaLog.status')">
                            <div v-if="form.status === 0">{{$t('operaLog.normal')}}</div>
                            <div v-else-if="form.status === 1">{{$t('operaLog.fail')}}</div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('operaLog.creatDate')">{{ parseTime(form.operTime) }}</el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item :label="$t('operaLog.expInfo')" v-if="form.status === 1">{{ form.errorMsg }}</el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="open = false" icon="el-icon-close">{{ $t('public.close') }}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { list } from '@/api/IOTData/IOTData'

export default {
    name: 'Operlog',
    dicts: ['sys_oper_type', 'sys_common_status'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            list: [],
            // 是否显示弹出层
            open: false,
            // 日期范围
            dateRange: [],
            // 默认排序
            defaultSort: { prop: 'operTime', order: 'descending' },
            // 表单参数
            form: {},
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                title: undefined,
                operName: undefined,
                businessType: undefined,
                status: undefined,
            },
            fullscreen: false,
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询登录日志 */
        getList() {
            this.loading = true
            list(this.addDateRange(this.queryParams, this.dateRange)).then(
                (response) => {
                    this.list = response.rows
                    this.total = response.total
                    this.loading = false
                }
            )
        },
        // 操作日志类型字典翻译
        typeFormat(row, column) {
            return this.selectDictLabel(
                this.dict.type.sys_oper_type,
                row.businessType
            )
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = []
            this.resetForm('queryForm')
            this.$refs.tables.sort(
                this.defaultSort.prop,
                this.defaultSort.order
            )
            this.handleQuery()
        },
        /** 多选框选中数据 */
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.operId)
            this.multiple = !selection.length
        },
        /** 排序触发事件 */
        handleSortChange(column, prop, order) {
            this.queryParams.orderByColumn = column.prop
            this.queryParams.isAsc = column.order
            this.getList()
        },
        /** 详细按钮操作 */
        handleView(row) {
            this.open = true
            this.form = row
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const operIds = row.operId || this.ids
            this.$modal
                .confirm(
                    this.$t('public.preConfirm', {
                        key: this.$t('operaLog.logCode'),
                        value: operIds,
                    })
                )
                .then(function () {
                    return delOperlog(operIds)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess(this.$t('operaLog.delSuc'))
                })
                .catch(() => {})
        },
        /** 清空按钮操作 */
        handleClean() {
            this.$modal
                .confirm(this.$t('operaLog.promptTip'))
                .then(function () {
                    return cleanOperlog()
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess(this.$t('operaLog.clearSuc'))
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'system/saas/operlog/export',
                {
                    ...this.queryParams,
                },
                `operlog_${new Date().getTime()}.xlsx`
            )
        },
    },
}
</script>

